package services

import (
	"errors"
	"im/app/conf"
	"im/app/models"
	"time"
)

type UserGroupService struct {

}

/**
	@op 添加群
	@params gName string 群名
    @params userId int 用户id
 */
func (usg *UserGroupService) Add(gName string, userId int)  error {
	userGroup := models.UserGroup{}
	//conf.Mdb.Where("g_name=?", gName).First(&userGroup)
	tx := conf.Mdb.Begin()
	tx.Where("g_name=?", gName).First(&userGroup)
	if userGroup.ID > 0 {
		tx.Rollback()
		return errors.New("数据已存在")
	}
	userGroup.GName = gName
	userGroup.UserID = userId
	userGroup.CreatedAt = time.Now().Unix()
	userGroup.UpdatedAt = time.Now().Unix()
	err := tx.Create(&userGroup).Error
	if err != nil {
		tx.Rollback()
		return errors.New("插入错误")
	}
	// 插入用户与群关系表
	userGroupRelation := models.UserGroupRelation{
		UserID:      userId,
		UserGroupID: userGroup.ID,
	}
	err = tx.Create(userGroupRelation).Error
	if err != nil {
		tx.Rollback()
		return errors.New("插入错误")
	}
	tx.Commit()

	return nil
}

/**
 * @op 通过用户id获取所有用户组id
 * @Params userId int 用户id
 * @return []int
 **/
func (usg *UserGroupService) GetUserGroupsByUserId(userId int) []int{
	user := models.User{}
	conf.Mdb.Where("id=?",userId).First(&user)
	conf.Mdb.Model(&user).Related(&user.UserGroupS,"UserGroupS").Find(&user)
	groupIds := make([]int, 0)
	for _,v := range user.UserGroupS {
		groupIds = append(groupIds, v.ID)
	}

	return groupIds
}

func (usg *UserGroupService) JoinGroup(gId int, userId int, gName string) error {
	if gName != "" {
		return usg.JoinGroupByName(gName, userId)
	}else {
		return usg.joinGroupByGid(gId, userId)
	}
}

/**
 * @op 通过群名加入群
 * @Params gName string 群名
 * @Params userId int 用户id
 * @return
 **/
func (usg *UserGroupService) JoinGroupByName (gName string, userId int) error {
	userGroup := models.UserGroup{}
	err := conf.Mdb.Where("g_name = ?",gName).First(&userGroup).Error
	if err != nil {
		return errors.New("加入群失败")
	}
	userGroupRelation := models.UserGroupRelation{
		UserID:      userId,
		UserGroupID: userGroup.ID,
	}
	err = conf.Mdb.Create(&userGroupRelation).Error
	if err != nil {
		return errors.New("加入群失败")
	}

	return nil
}

/**
 * @op 通过id加入群
 * @Params gId int 群id
 * @Params userId int 用户id
 * @return
 **/
func (usg *UserGroupService) joinGroupByGid (gId int, userId int) error {
	userGroup := models.UserGroup{}
	err := conf.Mdb.Where("id = ?",gId).First(&userGroup).Error
	if err != nil {
		return errors.New("加入群失败")
	}
	// 判断是否加入过群
	if userGroup.ID > 0 {
		return errors.New("已加入该群，不能重复加入")
	}
	userGroupRelation := models.UserGroupRelation{
		UserID:      userId,
		UserGroupID: userGroup.ID,
	}
	err = conf.Mdb.Create(&userGroupRelation).Error
	if err != nil {
		return errors.New("加入群失败")
	}

	return nil
}

/**
 * @op 通过群名退出群
 * @Params gName string 群名
 * @Params userId int 用户id
 * @return
 **/
func (usg *UserGroupService) OutGroupByName(gName string, userId int) error {
	userGroup := models.UserGroup{}
	err := conf.Mdb.Where("g_name = ?",gName).First(&userGroup).Error
	if err != nil {
		return errors.New("退出群失败")
	}
	userGroupRelation := models.UserGroupRelation{}
	err = conf.Mdb.Where("user_id=?",userId).Where("user_group_id=?",userGroup.ID).Delete(&userGroupRelation).Error
	if err != nil {
		return errors.New("退出群失败")
	}

	return nil
}

func (usg *UserGroupService) OutGroupByGid (gId int, userId int) error  {
	userGroup := models.UserGroup{}
	err := conf.Mdb.Where("id = ?",gId).First(&userGroup).Error
	if err != nil {
		return errors.New("退出群失败")
	}
	userGroupRelation := models.UserGroupRelation{}
	err = conf.Mdb.Where("user_id=?",userId).Where("user_group_id=?",userGroup.ID).Delete(&userGroupRelation).Error
	if err != nil {
		return errors.New("退出群失败")
	}

	return nil
}

// 搜索群
func (usg *UserGroupService) Search(gName string, page int, pageSize int) []*models.UserGroup{
	pageVar := (page-1)*pageSize
	userGroups := []*models.UserGroup{}
	err := conf.Mdb.Where("g_name like ?",gName+"%").Select("id,g_name").Limit(pageSize).Offset(pageVar).Find(&userGroups).Error
	if err != nil {
		return userGroups
	}

	return userGroups
}
